package cn.jsony.system.controller;

import cn.jsony.basic.util.AjaxResult;
import cn.jsony.basic.util.PageList;
import cn.jsony.system.domain.SystemDictionaryType;
import cn.jsony.system.query.SystemDictionaryTypeQuery;
import cn.jsony.system.service.ISystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController//@responsebody+@controller
@RequestMapping("/systemdictionarytype")
public class SystemDictionaryTypeController {
    @Autowired
    private ISystemDictionaryTypeService service;

    //查所有-get
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<SystemDictionaryType> findAll(){
        return service.findAll();
    }




    @GetMapping("/{id}")//查找一个get
    @ApiOperation(value = "通过ID查询" ) //：打在方法上，对方法做描述
    public SystemDictionaryType findOne(@PathVariable("id") Long id){
        return (SystemDictionaryType) service.findOne(id);

    }
    //分页查询
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery systemDictionaryTypeQuery){

        return service.queryPage(systemDictionaryTypeQuery);
    }

    //删除
    @ApiOperation(value = "删除" )
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            service.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    //批量删除
    @ApiOperation(value = "批量删除" )
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            service.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }




    //修改and添加
    @ApiOperation(value = "修改and添加" )
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemDictionaryType){
        try {
            if(systemDictionaryType.getId()==null){
                service.add(systemDictionaryType);
            }else {
                service.update(systemDictionaryType);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
}
